<?php
    Class Solution {
        /**
         * 747. 至少是其他数字两倍的最大数
         * @param Integer[] $nums
         * @return Integer
         */
        function dominantIndex747($nums) {
            $max = [[0,0],[0,0]];
            foreach($nums as $key => $num){
                if($num > $max[0][1]){
                    $max[1] = $max[0];
                    $max[0] = [$key,$num];
                }elseif($num > $max[1][1]){
                    $max[1] = [$key,$num];
                }
            }
            return $max[0][1] >= 2 * $max[1][1] ? $max[0][0]:-1;
        }

        /**
         * 748. 最短补全词
         * @param String $licensePlate
         * @param String[] $words
         * @return String
         */
        function shortestCompletingWord748($licensePlate, $words) {
            $lp = strtolower($licensePlate);
            $map = [];
            for($i = 0; $i < strlen($lp); $i++){
                if($lp[$i] >= 'a' && $lp[$i] <= 'z'){
                    if(isset($map[$lp[$i]])){
                        $map[$lp[$i]]++;
                    }else{
                        $map[$lp[$i]] = 1;
                    }
                }
            }
            foreach($words as $key => $word){
                $map2 = $map;
                for($i = 0; $i < strlen($word); $i++){
                    if($map2[$word[$i]]){
                        $map2[$word[$i]]--;
                    }
                }
                rsort($map2);
                if($map2[0] <= 0){
                    if(!isset($wow)){
                        $wow = $word;
                    }elseif(strlen($wow) > strlen($word)){
                        $wow = $word;
                    }
                }
            }
            return $wow;
        }

        /**
         * 766. 托普利茨矩阵
         * @param Integer[][] $matrix
         * @return Boolean
         */
        function isToeplitzMatrix766($matrix) {
            $func = function($num1,$num2) use (&$func,$matrix){
                if(isset($matrix[$num1][$num2])){
                    if($matrix[$num1][$num2] != $matrix[$num1-1][$num2-1]){
                        return false;
                    }else{
                        return $func($num1 + 1, $num2 + 1);
                    }
                }
                return true;
            };
            $count1 = count($matrix);
            $i = $count1 - 1;
            $j = 0;
            while(isset($matrix[$i][$j])){
                $check = $func($i + 1,$j + 1);
                if(!$check){
                    return false;
                }
                if($i > 0){
                    $i--;
                }else{
                    $j++;
                }
            }

            return true;
        }

        /**
         * 771. 宝石与石头
         * @param String $jewels
         * @param String $stones
         * @return Integer
         */
        function numJewelsInStones771($jewels, $stones) {
            $jmap = [];
            for($i = 0;$i < strlen($jewels);$i++){
                if(!$jmap[$jewels[$i]]){
                    $jmap[$jewels[$i]] = 1;
                }
            }
            $count = 0;
            for($i = 0;$i < strlen($stones);$i++){
                if($jmap[$stones[$i]]){
                    $count++;
                }
            }

            return $count;
        }

        /**
         * 796. 旋转字符串
         * @param String $s
         * @param String $goal
         * @return Boolean
         */
        function rotateString796($s, $goal) {
            if(strlen($s) != strlen($goal)){
                return false;
            }
            for($i = 0;$i < strlen($s);$i++){
                if(mb_substr($s, $i, strlen($s)) . mb_substr($s, 0, $i) == $goal){
                    return true;
                }
            }

            return false;
        }

        /**
         * 804. 唯一摩尔斯密码词
         * @param String[] $words
         * @return Integer
         */
        function uniqueMorseRepresentations804($words) {
            $map = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."];
            $check = [];
            foreach($words as $item){
                $str = '';
                for($i = 0;$i < strlen($item);$i++){
                    $str .= $map[ord($item[$i]) - ord('a')];
                }
                if(!$check[$str]){
                    $check[$str] = 1;
                }
            }

            return count($check);
        }

        /**
         * 806. 写字符串需要的行数
         * @param Integer[] $widths
         * @param String $s
         * @return Integer[]
         */
        function numberOfLines806($widths, $s) {
            $row = 0;
            $cur = 0;
            for($i = 0;$i < strlen($s);$i++){
                if($cur + $widths[ord($s[$i]) - ord('a')] > 100){
                    $row++;
                    $cur = 0;
                }
                $cur += $widths[ord($s[$i]) - ord('a')];
            }

            return[$row+1,$cur];
        }

        function mostCommonWord819($paragraph, $banned)
        {
            $separators = ['!','?',',',';','.',':','"','\'','(',')','[',']','{','}','<','>','/','\\','|','*','+','-','_','=','&','^','%','$','#','@','~','`','\r','\n'];
            $paragraph = str_replace($separators,' ',$paragraph);
            $paragraph = preg_replace('/\s+/', ' ', $paragraph);
            $paragraph = trim($paragraph);
            $list = explode(' ', $paragraph);
            $map = [];
            foreach($list as $item){
                $item = strtolower($item);
                if(!$map[$item]){
                    $map[$item] = 1;
                }else{
                    $map[$item]++;
                }
            }
            arsort($map);
            foreach($map as $key => $value){
                if(!in_array($key, $banned)){
                    return $key;
                }
            }

            return false;
        }

        /**
         * 821. 字符的最短距离
         * @param String $s
         * @param String $c
         * @return Integer[]
         */
        function shortestToChar821($s, $c) {
            $map   = [];
            for($i = 0;$i < strlen($s);$i++){
                if($s[$i] == $c){
                    $map[] = $i;
                }
            }
            $outMap = [];
            $check = $map[0];
            for($i = 0;$i < strlen($s);$i++){
                if(isset($map[1]) && ($i >= ($map[0] + $map[1])/2)){
                    array_shift($map);
                    $check = $map[0];
                }
                $outMap[] = abs($i - $check);
            }

            return $outMap;
        }

        /**
         * 824. 山羊拉丁文
         * @param String $sentence
         * @return String
         */
        function toGoatLatin824($sentence) {
            $list = explode(' ',$sentence);
            foreach($list as $key => &$item){
                if(in_array(strtolower($item[0]),['a','e','i','o','u'])){
                    $item = $item . 'ma' . str_repeat('a', $key + 1);
                }else{
                    $s = $item[0];
                    $item = substr($item,1) . $s .'ma' . str_repeat('a',$key + 1);
                }
            }

            return implode(' ',$list);
        }


    }

    $solution = new Solution();
$str = 'I speak Goat Latin';
print_r($solution->toGoatLatin824($str))



?>

